Skip to content

fix(install): clean up the temp dir when a package-manager install fails#1949

Merged
fengmk2 merged 3 commits into
mainfrom
06-26-fix_install_clean_up_the_temp_dir_when_a_package-manager_install_fails
Jun 27, 2026
Merged

fix(install): clean up the temp dir when a package-manager install fails#1949
fengmk2 merged 3 commits into
mainfrom
06-26-fix_install_clean_up_the_temp_dir_when_a_package-manager_install_fails

Conversation

@shulaoda

Copy link
Copy Markdown
Member

Problem

Both download_package_manager (pnpm/npm/yarn) and download_bun_package_manager created their temporary install directory like this:

let target_dir_tmp = tempfile::tempdir_in(parent_dir)?.path().to_path_buf();

tempfile::tempdir_in returns a TempDir guard whose Drop deletes the directory. Here the guard is an unnamed temporary that drops at the end of the let, so the directory is deleted immediately and only the PathBuf survives.

It still worked on the happy path because download_and_extract_tgz_with_hash recreates the directory (create_dir_all) before writing. But on any failure after that — a download/extract error, or the package → bin rename — the function returns early via ?, leaving the recreated temp dir behind. With the guard already gone, nothing cleans it up, so a .tmpXXXX directory leaks into $VP_HOME/package_manager/<name>/. Every failed install (flaky network, corrupt download, …) leaks one, and they accumulate forever.

Fix

Bind the TempDir to a named tmp_dir that lives for the whole function:

let tmp_dir = tempfile::tempdir_in(parent_dir)?;
let target_dir_tmp = tmp_dir.path().to_path_buf();

Now any early return drops the guard and removes the temp dir. On success the dir is renamed into its final location first, so the end-of-scope drop hits a NotFound on the original path, which tempfile ignores — success is unaffected. Applied to both sites.

Test

test_download_package_manager_cleans_temp_dir_on_failure drives download_package_manager(Pnpm, "10.0.0", None) against a mock registry serving a non-gzip tarball (the download succeeds, extraction fails), then asserts no leftover directory remains under package_manager/pnpm/. It fails on the old code (a leaked .tmp* dir) and passes on the fix. The full package_manager module stays green (91 passed).

Reproduce

cargo test -p vite_install --lib \
  package_manager::tests::test_download_package_manager_cleans_temp_dir_on_failure
# If you run a local/system HTTP proxy, prefix with NO_PROXY=127.0.0.1,localhost
# so the loopback mock isn't intercepted (same requirement as the other httpmock
# tests in this crate).

To watch the bug itself: temporarily revert the two sites to let target_dir_tmp = tempfile::tempdir_in(parent_dir)?.path().to_path_buf(); and rerun — the test fails with a leaked .tmpXXXX directory under package_manager/pnpm/.

🤖 Generated with Claude Code

@netlify

netlify Bot commented Jun 25, 2026

Copy link
Copy Markdown

Deploy Preview for viteplus-preview canceled.

Name Link
🔨 Latest commit 721330b
🔍 Latest deploy log https://app.netlify.com/projects/viteplus-preview/deploys/6a3fe2ca9cba6200082e5220

Comment thread crates/vite_install/src/package_manager.rs

@fengmk2 fengmk2 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@shulaoda Thanks!

@fengmk2 fengmk2 merged commit f2ee2df into main Jun 27, 2026
40 checks passed
@fengmk2 fengmk2 deleted the 06-26-fix_install_clean_up_the_temp_dir_when_a_package-manager_install_fails branch June 27, 2026 14:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants